<?php

class Http {
    public static $_instance = null;
    public $isPost =  false;
    public static function instance(){
        if( is_null( self::$_instance) ) {
            self::$_instance = new static();
        }
        return self::$_instance;
    }
    public  function request($url,$method = 'GET',$param=[],$headers=[]){
        $method = strtoupper($method);
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if($method=='POST'){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //这个是重点,规避ssl的证书检查。
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 跳过host验证

        $this->data = curl_exec($ch);
        curl_close($ch);
        return $this;

    }

    public function data($callback=null){
    	   if($callback instanceof \Closure){
    	   	   call_user_func($callback,$this->data);
    	   }else{
    	   	   return $this->data;
    	   }
           
    }
    public function toArray(){
        try{
            return json_decode( $this->data , true );
        }catch (\Exception $e){
            return [];
        }
    }

 
}



